# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@rules_proto//proto:defs.bzl", "proto_library")
load("@rules_cc//cc:defs.bzl", "cc_library", "cc_proto_library", "cc_test")
load("@hedron_compile_commands//:refresh_compile_commands.bzl", "refresh_compile_commands")

COPTS = [
    "-D__STDC_FORMAT_MACROS",
    "-DBTHREAD_USE_FAST_PTHREAD_MUTEX",
    "-D__const__=__unused__",
    "-D_GNU_SOURCE",
    "-DUSE_SYMBOLIZE",
    "-DNO_TCMALLOC",
    "-D__STDC_LIMIT_MACROS",
    "-D__STDC_CONSTANT_MACROS",
    "-fPIC",
    "-Wno-unused-parameter",
    "-fno-omit-frame-pointer",
    "-fno-access-control",
    "-DBAZEL_TEST=1",
    "-DBVAR_NOT_LINK_DEFAULT_VARIABLES",
    "-DUNIT_TEST",
] + select({
    "//bazel/config:brpc_with_glog": ["-DBRPC_WITH_GLOG=1"],
    "//conditions:default": ["-DBRPC_WITH_GLOG=0"],
})

TEST_BUTIL_SOURCES = [
    "at_exit_unittest.cc",
    "atomicops_unittest.cc",
    "base64_unittest.cc",
    "base64url_unittest.cc",
    "big_endian_unittest.cc",
    "bits_unittest.cc",
    "hash_tables_unittest.cc",
    "linked_list_unittest.cc",
    "mru_cache_unittest.cc",
    "small_map_unittest.cc",
    "stack_container_unittest.cc",
    "mpsc_queue_unittest.cc",
    "cpu_unittest.cc",
    "crash_logging_unittest.cc",
    "leak_tracker_unittest.cc",
    "stack_trace_unittest.cc",
    "environment_unittest.cc",
    "file_util_unittest.cc",
    "dir_reader_posix_unittest.cc",
    "file_path_unittest.cc",
    "file_unittest.cc",
    "scoped_temp_dir_unittest.cc",
    "guid_unittest.cc",
    "hash_unittest.cc",
    "lazy_instance_unittest.cc",
    "aligned_memory_unittest.cc",
    "linked_ptr_unittest.cc",
    "ref_counted_memory_unittest.cc",
    "ref_counted_unittest.cc",
    "scoped_ptr_unittest.cc",
    "scoped_vector_unittest.cc",
    "singleton_unittest.cc",
    "weak_ptr_unittest.cc",
    "observer_list_unittest.cc",
    "file_descriptor_shuffle_unittest.cc",
    "rand_util_unittest.cc",
    "safe_numerics_unittest.cc",
    "scoped_clear_errno_unittest.cc",
    "scoped_generic_unittest.cc",
    "security_unittest.cc",
    "sha1_unittest.cc",
    "stl_util_unittest.cc",
    "nullable_string16_unittest.cc",
    "safe_sprintf_unittest.cc",
    "string16_unittest.cc",
    "stringprintf_unittest.cc",
    "string_number_conversions_unittest.cc",
    "string_piece_unittest.cc",
    "string_split_unittest.cc",
    "string_tokenizer_unittest.cc",
    "string_util_unittest.cc",
    "stringize_macros_unittest.cc",
    "sys_string_conversions_unittest.cc",
    "utf_offset_string_conversions_unittest.cc",
    "utf_string_conversions_unittest.cc",
    "cancellation_flag_unittest.cc",
    "condition_variable_unittest.cc",
    "lock_unittest.cc",
    "waitable_event_unittest.cc",
    "type_traits_unittest.cc",
    "non_thread_safe_unittest.cc",
    "platform_thread_unittest.cc",
    "simple_thread_unittest.cc",
    "thread_checker_unittest.cc",
    "thread_collision_warner_unittest.cc",
    "thread_id_name_manager_unittest.cc",
    "thread_local_storage_unittest.cc",
    "thread_local_unittest.cc",
    "watchdog_unittest.cc",
    "time_unittest.cc",
    "version_unittest.cc",
    "logging_unittest.cc",
    "cacheline_unittest.cpp",
    "class_name_unittest.cpp",
    "endpoint_unittest.cpp",
    "unique_ptr_unittest.cpp",
    "errno_unittest.cpp",
    "fd_guard_unittest.cpp",
    "file_watcher_unittest.cpp",
    "find_cstr_unittest.cpp",
    "scoped_lock_unittest.cpp",
    "status_unittest.cpp",
    "string_printf_unittest.cpp",
    "string_splitter_unittest.cpp",
    "synchronous_event_unittest.cpp",
    "temp_file_unittest.cpp",
    "baidu_thread_local_unittest.cpp",
    "thread_key_unittest.cpp",
    "baidu_time_unittest.cpp",
    "flat_map_unittest.cpp",
    "crc32c_unittest.cc",
    "iobuf_unittest.cpp",
    "object_pool_unittest.cpp",
    "test_switches.cc",
    "scoped_locale.cc",
    "recordio_unittest.cpp",
    #"popen_unittest.cpp",
    "bounded_queue_unittest.cc",
    "butil_unittest_main.cpp",
    "scope_guard_unittest.cpp",
    "optional_unittest.cpp",
] + select({
    "@bazel_tools//tools/osx:darwin_x86_64": [],
    "//conditions:default": [
        "test_file_util_linux.cc",
        "proc_maps_linux_unittest.cc",
    ],
})

proto_library(
    name = "test_proto",
    srcs = glob(
        [
            "*.proto",
        ],
    ),
    strip_import_prefix = "/test",
    visibility = ["//visibility:public"],
    deps = [
        "//:brpc_idl_options_proto",
    ]
)

cc_proto_library(
    name = "cc_test_proto",
    visibility = ["//visibility:public"],
    deps = [
        ":test_proto",
    ],
)

cc_library(
    name = "sstream_workaround",
    hdrs = [
        "sstream_workaround.h",
    ],
)

cc_library(
    name = "gperftools_helper",
    hdrs = [
        "gperftools_helper.h",
    ],
)

cc_test(
    name = "butil_test",
    srcs = TEST_BUTIL_SOURCES + [
        "scoped_locale.h",
        "multiprocess_func_list.h",
        "test_switches.h",
    ],
    copts = COPTS,
    deps = [
        ":cc_test_proto",
        ":sstream_workaround",
        ":gperftools_helper",
        "//:brpc",
        "@com_google_googletest//:gtest",
    ],
)


cc_test(
    name = "bvar_test",
    srcs = glob(
        [
            "bvar_*_unittest.cpp",
        ],
        exclude = [
            "bvar_lock_timer_unittest.cpp",
            "bvar_recorder_unittest.cpp",
        ],
    ),
    copts = COPTS,
    deps = [
        ":sstream_workaround",
        ":gperftools_helper",
        "//:bvar",
        "@com_google_googletest//:gtest",
    ],
)

cc_test(
    name = "bthread_test",
    srcs = glob(
        [
            "bthread_*_unittest.cpp",
        ],
        exclude = [
            "bthread_cond_unittest.cpp",
            "bthread_execution_queue_unittest.cpp",
            "bthread_dispatcher_unittest.cpp",
            "bthread_fd_unittest.cpp",
            "bthread_mutex_unittest.cpp",
            "bthread_setconcurrency_unittest.cpp",
            # glog CHECK die with a fatal error
            "bthread_key_unittest.cpp",
            "bthread_butex_multi_tag_unittest.cpp",
            "bthread_rwlock_unittest.cpp",
            "bthread_semaphore_unittest.cpp",
        ],
    ),
    copts = COPTS,
    deps = [
        ":sstream_workaround",
        ":gperftools_helper",
        "//:brpc",
        "@com_google_googletest//:gtest",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_test(
    name = "brpc_prometheus_test",
    srcs = glob(
        [
            "brpc_prometheus_*_unittest.cpp",
        ],
    ),
    copts = COPTS,
    deps = [
        ":cc_test_proto",
        ":sstream_workaround",
        "//:brpc",
        "@com_google_googletest//:gtest",
        "@com_google_googletest//:gtest_main",
    ],
)

refresh_compile_commands(
    name = "brpc_test_compdb",
    # Specify the targets of interest.
    # For example, specify a dict of targets and their arguments:
    targets = {
        "//:brpc": "",
        ":bvar_test": "",
        ":bthread_test": "",
        ":butil_test": "",
    },
    # For more details, feel free to look into refresh_compile_commands.bzl if you want.
)
